package com.heliang.ui.info;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import android.app.Activity;
import android.content.Context;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.appxiehui2.R;
import com.heliang.common.FileLog;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.util.AppUtil;
import com.heliang.util.Constants;
import com.heliang.util.ImageLoaderUtil;

public class InfoListAdapter extends BaseAdapter {

    private List<InfoBean> itemBeanList;
    private Activity activity;

    public void setItemBeanList(List<InfoBean> itemBeanList) {
        this.itemBeanList = itemBeanList;
    }

    public int getCount() {
        return itemBeanList.size();
    }

    public InfoListAdapter(Activity activity, List<InfoBean> itemBeanList, Context context) {
        this.itemBeanList = itemBeanList;
        this.activity = activity;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        // 设定布局 the views from XML
        View rowView = convertView;
        rowView = null;
        if (rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            try {
                rowView = inflater.inflate(R.layout.info_list_item, null);
            } catch (Exception e) {
                // TODO: handle exception
                Log.e(Constants.TAG, e.getMessage());
            }
        }
        try {
            rowView.setBackgroundResource(R.drawable.list_item_seletor);
            FrameLayout ll = (FrameLayout) rowView.findViewById(R.id.list_item_head_layout);

            TextView titleTv = (TextView) rowView.findViewById(R.id.recommend_list_item_title_textview);
            TextPaint tp = titleTv.getPaint();
            tp.setFakeBoldText(true);

            TextView descTv = (TextView) rowView.findViewById(R.id.recommend_list_item_desc_textview);
            ImageView pushIconIv = (ImageView) rowView.findViewById(R.id.push_icon);
            ImageView headIv = (ImageView) rowView.findViewById(R.id.list_item_head_image);

            descTv.setLines(2);

            InfoBean infoBean = this.itemBeanList.get(position);

            pushIconIv.setVisibility(View.VISIBLE);
            if (!infoBean.isRecommend()) {
                pushIconIv.setVisibility(View.GONE);
            }
            if (infoBean.isFirstNews()) {// 头条
                pushIconIv.setVisibility(View.VISIBLE);
                pushIconIv.setImageResource(R.drawable.is_hot);
            }

            if (infoBean.isRead()) {
                titleTv.setTextColor(activity.getResources()
                    .getColor(R.color.info_list_item_has_read_title));
                descTv.setTextColor(activity.getResources().getColor(R.color.info_list_item_has_read_desc));
            }

            if (infoBean.getPic2Url() != null && !infoBean.getPic2Url().equals("")) {
                headIv.setBackgroundDrawable(activity.getResources().getDrawable(
                    R.drawable.info_default_icon));
                FinalBitmap finalBitMap = FinalBitmap.create(activity);
                finalBitMap.configLoadingImage(R.drawable.info_default_icon);
                finalBitMap.display(headIv, infoBean.getPic2Url());
            }

            //
            // if (infoBean.getPic2Url() != null &&
            // !infoBean.getPic2Url().equals("")) {
            //
            //
            // ll.setVisibility(View.VISIBLE);
            // headIv.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.defaulte_head_icon));
            // ImageLoaderUtil.instance.setImageDrawable(infoBean.getPic2Path(),
            // infoBean.getPic2Url(),headIv,true);
            // rowView.setTag(R.id.list_item_head_image, headIv);
            // }else {
            // // ll.setVisibility(View.GONE);
            // }

            int titleWordCount = AppUtil.getWordCount((int) ((Constants.SCREEN_WIDTH
                - headIv.getDrawable().getIntrinsicWidth() - 62)), titleTv.getTextSize()) - 2;// -2是去除省略号占用的长度
            int descWordCount = AppUtil.getWordCount((int) ((Constants.SCREEN_WIDTH
                - headIv.getDrawable().getIntrinsicWidth() - 62) * 2), descTv.getTextSize()) - 2;

            String titleStr = infoBean.getTitle();
            String descStr = infoBean.getContent();

            if (infoBean.getTitle().length() > titleWordCount) {
                titleStr = infoBean.getTitle().substring(0, titleWordCount) + "...";
            }

            if (infoBean.getContent().length() > descWordCount) {
                descStr = infoBean.getContent().substring(0, descWordCount) + "...";
            }
            titleTv.setText(titleStr);
            descTv.setText(descStr);
            rowView.setTag(infoBean);
        } catch (Exception e) {
            // TODO: handle exception
            FileLog.log("HotInfoListAdapter.getView " + e.getMessage());
        }

        return rowView;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

}
